﻿using System;
using System.Linq;
using Nop.Core.Domain.Tasks;
using Nop.Core.Plugins;
using Nop.Plugin.Misc.DynamicsCrm.Data;
using Nop.Services.Localization;
using Nop.Services.Tasks;

namespace Nop.Plugin.Misc.DynamicsCrm.Services
{
    public class DynamicsCrmInstallationService
    {
        private readonly DynamicsCrmObjectContext _dynamicsCrmObjectContext;
        private readonly IScheduleTaskService _scheduleTaskService;

        public DynamicsCrmInstallationService(DynamicsCrmObjectContext dynamicsCrmObjectContext,
            IScheduleTaskService scheduleTaskService)
        {
            _dynamicsCrmObjectContext = dynamicsCrmObjectContext;
            _scheduleTaskService = scheduleTaskService;
        }

        /// <summary>
        /// Installs the sync task.
        /// </summary>
        private void InstallSyncTask()
        {
            //Check the database for the task
            var task = FindScheduledTask();

            if (task == null)
            {
                task = new ScheduleTask
                {
                    Name = "DynamicsCrm sync",
                    //each 60 minutes
                    Seconds = 3600,
                    Type = "Nop.Plugin.Misc.DynamicsCrm.DynamicsCrmSynchronizationTask, Nop.Plugin.Misc.DynamicsCrm",
                    Enabled = false,
                    StopOnError = false,
                };
                _scheduleTaskService.InsertTask(task);
            }
        }

        private ScheduleTask FindScheduledTask()
        {
            return _scheduleTaskService.GetTaskByType("Nop.Plugin.Misc.DynamicsCrm.DynamicsCrmSynchronizationTask, Nop.Plugin.Misc.DynamicsCrm");
        }

        /// <summary>
        /// Installs this instance.
        /// </summary>
        /// <param name="plugin">The plugin.</param>
        public virtual void Install(BasePlugin plugin)
        {
            //locales
            plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.DynamicsCrm.UserName", "Dynamics Crm User Name");
            plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.DynamicsCrm.OrganizationUri", "Organization Uri");
            plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.DynamicsCrm.EmailErrorsTo", "Send errors to this email");
            plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.DynamicsCrm.CustomerTypeCode", "CustomerTypeCode value comes from personalisation area in the CRM");
            plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.DynamicsCrm.PriceLevel", "Default PriceLevel name comes from product catalog in parameters area in the CRM");
            plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.DynamicsCrm.Uom", "Default Uom name comes from product catalog in parameters area in the CRM");
            plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.DynamicsCrm.UomSchedule", "Default UomSchedule name comes from product catalog in parameters area in the CRM");
            plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.DynamicsCrm.AutoSync", "Use AutoSync task");
            plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.DynamicsCrm.AutoSyncEachMinutes", "AutoSync task period (minutes)");
            plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.DynamicsCrm.AutoSyncRestart", "If sync task period has been changed, please restart the application");
            plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.DynamicsCrm.Password", "Password");
            plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.DynamicsCrm.QueueAllContacts", "Initial contact queue");
            plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.DynamicsCrm.QueueAllContacts.Hint", "Queue all existing contacts (run only once)");
            plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.DynamicsCrm.QueueAllOrders", "Initial orders queue");
            plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.DynamicsCrm.QueueAllOrders.Hint", "Queue all existing orders (run only once)");
            plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.DynamicsCrm.ManualSync", "Manual Sync");
            plugin.AddOrUpdatePluginLocaleResource("Plugin.Misc.DynamicsCrm.ManualSync.Hint", "Manually synchronize nopCommerce data with DynamicsCrm database");

            //Install sync task
            InstallSyncTask();

            //Install the database tables
            _dynamicsCrmObjectContext.Install();
        }

        /// <summary>
        /// Uninstalls this instance.
        /// </summary>
        /// <param name="plugin">The plugin.</param>
        public virtual void Uninstall(BasePlugin plugin)
        {
            //locales
            plugin.DeletePluginLocaleResource("Plugin.Misc.DynamicsCrm.UserName");
            plugin.DeletePluginLocaleResource("Plugin.Misc.DynamicsCrm.OrganizationUri");
            plugin.DeletePluginLocaleResource("Plugin.Misc.DynamicsCrm.EmailErrorsTo");
            plugin.DeletePluginLocaleResource("Plugin.Misc.DynamicsCrm.CustomerTypeCode");
            plugin.DeletePluginLocaleResource("Plugin.Misc.DynamicsCrm.PriceLevel");
            plugin.DeletePluginLocaleResource("Plugin.Misc.DynamicsCrm.Uom");
            plugin.DeletePluginLocaleResource("Plugin.Misc.DynamicsCrm.UomSchedule");
            plugin.DeletePluginLocaleResource("Plugin.Misc.DynamicsCrm.AutoSync");
            plugin.DeletePluginLocaleResource("Plugin.Misc.DynamicsCrm.AutoSyncEachMinutes");
            plugin.DeletePluginLocaleResource("Plugin.Misc.DynamicsCrm.AutoSyncRestart");
            plugin.DeletePluginLocaleResource("Plugin.Misc.DynamicsCrm.Password");
            plugin.DeletePluginLocaleResource("Plugin.Misc.DynamicsCrm.QueueAllContacts");
            plugin.DeletePluginLocaleResource("Plugin.Misc.DynamicsCrm.QueueAllContacts.Hint");
            plugin.DeletePluginLocaleResource("Plugin.Misc.DynamicsCrm.QueueAllOrders");
            plugin.DeletePluginLocaleResource("Plugin.Misc.DynamicsCrm.QueueAllOrders.Hint");
            plugin.DeletePluginLocaleResource("Plugin.Misc.DynamicsCrm.ManualSync");
            plugin.DeletePluginLocaleResource("Plugin.Misc.DynamicsCrm.ManualSync.Hint");

            //Remove scheduled task
            var task = FindScheduledTask();
            if (task != null)
                _scheduleTaskService.DeleteTask(task);

            //Uninstall the database tables
            _dynamicsCrmObjectContext.Uninstall();
        }
    }
}